package com.iworkh.spring.web.mvc.controller;

import com.iworkh.spring.web.mvc.domain.vo.Site;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * ModelAttributeController
 *
 * @author: iworkh-沐雨云楼
 * @date: 2019-12-27
 */
@Controller
@RequestMapping("/springmvc")
@SessionAttributes(value = {"siteDB"})
public class ModelAttributeController {

    // @ModelAttribute
    // public void getSite(String name, Map<String, Object> map) {
    //     System.out.println("ModelAttribute - getSiteMap");
    //     Site iworkSite = Site.builder().url("https://www.iworkh.com/1").name("iworkh1").build();
    //
    //     System.out.println("getSite:" + iworkSite);
    //     map.put("siteDB", iworkSite);
    // }

    @ModelAttribute("siteDB")
    public Site getSite() {
        System.out.println("ModelAttribute - getSite");
        Site iworkSite = Site.builder().url("https://www.iworkh.com/2").name("iworkh2").build();

        System.out.println("getSite:" + iworkSite);
        return iworkSite;
    }

    @RequestMapping("/getSite")
    public String getSite(Map<String, Object> map) {
        System.out.println("getSite");
        Site iworkSite = Site.builder().url("https://www.iworkh.com/3").name("iworkh3").build();

        System.out.println("getSite:" + iworkSite);
        map.put("siteDB", iworkSite);

        return "success";
    }

    @RequestMapping("/testModelAttribute")
    public ModelAndView testModelAttribute(@ModelAttribute("siteDB") Site site) {
        System.out.println("iworkh-testModelAttribute: " + site);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("site", site);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/testModelAttribute2")
    public String testModelAttribute2(String id) {
        System.out.println("iworkh-testModelAttribute2: " + id);

        return "success";
    }

}
